package Concurent;

import java.util.concurrent.CountDownLatch;

/**
 * Created by root on 17-6-7.
 */
public class countDownLatchDemo1 {
    public static void main(String[] args) throws InterruptedException {
        //闭锁 ，就是让线程都跑完了再执行后面的操作
        CountDownLatch cdl = new CountDownLatch(3);
        new Thread(new T3(cdl)).start();
        new Thread(new T4(cdl)).start();
        new Thread(new T5(cdl)).start();
        System.out.println(cdl);
        cdl.await();   //这个时候会阻塞，直到里面的Count变成0，目前为3，也就是说三个线程执行完成才会往下走
        System.out.println("开始做饭了");
    }
}

class T3 implements Runnable{
    private CountDownLatch cdl = null;
    public T3(CountDownLatch cdl) {
        this.cdl = cdl;
    }

    @Override
    public void run() {

        try {
            Thread.sleep(3000);
            System.out.println("T3买菜回来了");
            cdl.countDown();    //这个时候Count会-1
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class T4 implements Runnable{
    private CountDownLatch cdl = null;
    public T4(CountDownLatch cdl) {
        this.cdl = cdl;
    }

    @Override
    public void run() {

        try {
            Thread.sleep(4000);
            System.out.println("T4买米回来了");
            cdl.countDown();    //这个时候Count会-1
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class T5 implements Runnable{
    private CountDownLatch cdl = null;
    public T5(CountDownLatch cdl) {
        this.cdl = cdl;
    }
    @Override
    public void run() {
        try {
            Thread.sleep(5000);
            System.out.println("T5买锅回来了");
            cdl.countDown();     //这个时候Count会-1
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}